package com.acme.bankapp.domain.bank.account;

/**
 * This is abstract class which implements {@link Account} interface
 *
 * @author Alexander Kotov
 */

public abstract class AbstractAccount implements Account {

    private int id;
    protected double balance;

    public AbstractAccount(int id, double balance) throws IllegalArgumentException {
        super();
        if (balance < 0.) {
            throw new IllegalArgumentException("Negative balance");
        }
        this.id = id;
        this.balance = balance;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public double getBalance() {
        return balance;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void deposit(double amount) {
        balance += amount;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        if (!(obj instanceof AbstractAccount)) {
            return false;
        }
        AbstractAccount that = (AbstractAccount) obj;
        return (id == that.id);
    }

    @Override
    public int hashCode() {
        return id;
    }

    @Override
    public String toString() {
        return "Account balance = " + balance;
    }
}
